//封装倒计时逻辑函数
import { ref,computed, onUnmounted } from 'vue'
import dayjs from 'dayjs'
export const useCountDown = () => {
    //1.响应式数据
    const time=ref(0)
    let timer=null
    const formatTime=computed(()=>dayjs.unix(time.value).format('mm分ss') )
    //2.定时器
    const start=(currentTime)=>{
        time.value = currentTime
        timer = setInterval(() => {
            if (time.value > 0) {
                time.value--;
            } else {
                // 当时间到达0时，清除定时器
                clearInterval(timer);
                timer = null; // 清空timer变量，防止重复清除
            }
        }, 1000);
    }

    //3.清除定时器
    onUnmounted(()=>{
       timer&& clearInterval(timer)
    })
    return {
        formatTime,
        start
    }
}